package cn.stylefeng.guns.modular.rest.hxStorageStock.controller;

import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicGoods;
import cn.stylefeng.guns.modular.hx.basic.model.params.HxBasicGoodsParam;
import cn.stylefeng.guns.modular.hx.basic.model.result.HxBasicGoodsResult;
import cn.stylefeng.guns.modular.hx.basic.service.HxAppBasicGoodsService;
import cn.stylefeng.guns.modular.hx.order.entity.HxOrderOutDetailed;
import cn.stylefeng.guns.modular.hx.order.service.HxOrderOutDetailedService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageOut;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageOutDetailed;
import cn.stylefeng.guns.modular.hx.storage.mapper.HxStorageOutDetailedMapper;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageStockResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOutDetailedService;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOutService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.rest.hxStorageStock.service.HxAppStorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.Menu;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品查询
 */

@Controller
@RequestMapping("/rest/hxStorageStock")
public class HxAppStorageStockController extends BaseController {
    @Autowired
    private UserAop userAop;
    @Autowired
    private HxAppBasicGoodsService goodsService;
    @Autowired
    private HxAppStorageStockService stockService;
    @Autowired
    private HxStorageOutDetailedService hxStorageOutDetailedService;
    @Autowired
    private  HxOrderOutDetailedService hxOrderOutDetailedService;

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-08-15
    * */


    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStorageStockParam hxStorageStockParam) {
        //this.stockService.update(hxStorageStockParam);
        return ResponseData.success();
    }

    @RequestMapping("/findGoodsListBySpec")
    @ResponseBody
    public ResponseData findGoodsListBySpec(HxStorageStockParam hxStorageStockParam) {
        if (ToolUtil.isEmpty(hxStorageStockParam))
            return null;
        User user = this.userAop.getUser();
        hxStorageStockParam.setDeliverId(user.getDeliverId());
        hxStorageStockParam.setStockState("正常");
        List<HxStorageStockResult> result =this.stockService.findGoodsListBySpec(hxStorageStockParam, user.getDeptId(),user.getOperateId());
        return ResponseData.success(result);
    }

    /**
     * 查询商品
     **/
    @ResponseBody
    @RequestMapping("/goodslist")
    public ResponseData goodslist(HxBasicGoodsParam goodsParam, String condition ) {
        User user = this.userAop.getUser();
        goodsParam.setDeliverId(user.getDeliverId());
        if (ToolUtil.isEmpty(condition)){
            return null;
        }
        List<HxBasicGoodsResult> goodsResults=this.goodsService.findListBySpecApp(goodsParam, user.getDeptId(),condition,user.getOperateId());
        return ResponseData.success(goodsResults);
    }

    /**
     * 查询详情
     * @param hxStorageStockParam
     * @return
     */
    @RequestMapping("/findGoodsOneBySpec")
    @ResponseBody
    public ResponseData findGoodsOneBySpec(HxStorageStockParam hxStorageStockParam) {
        if (ToolUtil.isEmpty(hxStorageStockParam))
            return null;
        HxBasicGoods goodsOne = this.goodsService.getById(hxStorageStockParam.getGoodsId());
        return ResponseData.success(goodsOne);
    }

    /**
     * 商品信息维护
     * @param goodsParam
     * @return
     */
    @RequestMapping("/updateGoodsOneBySpec")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation= Propagation.REQUIRED,timeout=60)
    public ResponseData updateGoodsOneBySpec(HxBasicGoodsParam goodsParam) {
        User user = userAop.getUser();
        if (ToolUtil.isEmpty(goodsParam) || ToolUtil.isEmpty(goodsParam.getId())) {
            return ResponseData.error("请求参数不正确或商品ID为空");
        }
        try {
            // 根据ID查询商品信息
            HxBasicGoods goodsOne = goodsService.getById(goodsParam.getId());
            if (goodsOne == null) {
                return ResponseData.error("未找到对应的商品信息");
            }
            // 更新商品信息
            goodsOne.setGoodsXcode69(goodsParam.getGoodsXcode69());
            goodsOne.setGoodsHcode69(goodsParam.getGoodsHcode69());
            // 执行更新操作
            boolean updateCount = goodsService.updateById(goodsOne);
            Long deptId = user.getDeptId();
            HxStorageOutDetailedParam param = new HxStorageOutDetailedParam();
            param.setGoodsId(goodsParam.getId().longValue());
            param.setDeptId(deptId);
            param.setGoodsXcode69(goodsOne.getGoodsXcode69());
            param.setGoodsHcode69(goodsOne.getGoodsHcode69());

            // 更新拣货单详情表
            int updateCount2 =  hxStorageOutDetailedService.updateCode(param);
            // 更新出库订单详情表
            int updateCount3 = hxStorageOutDetailedService.updateOrder(param);

            if (updateCount && updateCount2 > 0 && updateCount3 > 0) {
                return ResponseData.success(202,"商品信息更新成功",null);
            } else {
                return ResponseData.error("商品信息未更新");
            }
        } catch (Exception e) {
            e.printStackTrace();
             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
            return ResponseData.error("系统异常，更新失败");
        }
    }

    @RequestMapping("/findKcListBySpec")
    @ResponseBody
    public ResponseData findKcListBySpec(HxStorageStockParam hxStorageStockParam) {
        if (ToolUtil.isEmpty(hxStorageStockParam))
            return null;
        User user = this.userAop.getUser();
        hxStorageStockParam.setDeliverId(user.getDeliverId());
//        hxStorageStockParam.setStockState("正常");
        List<HxStorageStockResult> results=this.stockService.findKcListBySpec(hxStorageStockParam,user.getDeptId(),user.getOperateId());
        return ResponseData.success(results);
    }

    /**
     * app搜索功能
     *
     * @author
     * @Date 2023-09-09
     */
    @ResponseBody
    @RequestMapping("/hxStorageMoveList")
    public ResponseData hxStorageMoveList(HxStorageStockParam hxStorageStockParam,String condition) {
        if (ToolUtil.isEmpty(hxStorageStockParam)&&ToolUtil.isEmpty(condition))
            return null;
        User user = this.userAop.getUser();
        List<HxStorageStockResult> results = this.stockService.hxStorageMoveList(hxStorageStockParam,condition,user);
        return ResponseData.success(results);
    }

    /**
     * app搜索功能
     *
     * @author
     * @Date 2023-09-09
     */
    @ResponseBody
    @RequestMapping("/hxStorageoffShelfList")
    public ResponseData hxStorageoffShelfList(HxStorageStockParam hxStorageStockParam,String condition) {
        if (ToolUtil.isEmpty(hxStorageStockParam)&&ToolUtil.isEmpty(condition))
            return null;
        User user = this.userAop.getUser();
        List<HxStorageStockResult> results = this.stockService.hxStorageoffShelfList(hxStorageStockParam,condition,user);
        return ResponseData.success(results);
    }
    
    @Autowired
    private UserService userService;
    
    /**
     * 修改库存日期
     * @return
     */

    @RequestMapping("/updateDetailedDate")
    @ResponseBody
    public ResponseData updateDetailedDate(HxStorageStockParam hxStorageStockParam) {
        //校验是否有加急置顶权限
        User user = userAop.getUser();
        List<Long> roleList = new ArrayList<Long>();
        String[] split = user.getRoleId().split(",");//以逗号分割
        for (String string2 : split) {
            roleList.add(Long.parseLong(string2));
        }
        List<Menu> mlist = userService.getAppletsPermissionlist(roleList,"/rest/hxStorageStock/updateDetailedDate");
        if(mlist.size() == 0) {
            return ResponseData.error("权限异常");
        }
     return this.stockService.updateDetailedDate(hxStorageStockParam);

    }
}
